/*
============================================================================
 Name        : $(Basename).cpp
 Author      : $(Author)
 Version     :
 Copyright   : $(Copyright)
 Description : Application source file
============================================================================

The following steps are taken when starting an application:
1. <Entry Point>::NewApplication() --> C$(Basename)Application
2. C$(Basename)Application::CreateDocument() --> C$(Basename)Document
3. C$(Basename)Document::CreateAppUiL() --> C$(Basename)AppUi
4. C$(Basename)AppUi::ConstructL() --> C$(Basename)Container

Symbian GUI applications follow the Model-View-Controller (MVC) pattern.
- C$(Basename)Document is the Model that holds the application data
- C$(Basename)Container is the View that displays the application data
- C$(Basename)AppUi is the Controller that coordinates user actions/views
While the naming may seem a bit misleading (the names are according to the
current Symbian conventions), it is important to understand how they relate
to the MVC pattern.

Note! Normally you would have a separate file for each of these classes, but
here, for the sake of simplicity, they are all put in the same file. Having
the classes defined in separate files makes the source code easier to maintain.

*/

// INCLUDES
#include "$(Basename).h"

// CONSTANTS
const TUid KUid$(Basename)App = { $(uid) }; // Application UID

// MEMBER FUNCTIONS

//----------------------------------------------------------------------------
// Application entry point
//----------------------------------------------------------------------------

// GLOBAL FUNCTIONS

// DLL entry point, needed for DLL initialization.
// Usually there is no need to initialize anything, so we just
// return that everything is ok
GLDEF_C TInt E32Dll(TDllReason /*aReason*/) {
    return KErrNone;
}

// EXPORTED FUNCTIONS

// Static function for instantiating a new application object.
// Called by the Symbian application framework.
EXPORT_C CApaApplication* NewApplication() {
    return new C$(Basename)Application;
}

//----------------------------------------------------------------------------
// C$(Basename)Application
//----------------------------------------------------------------------------

// Creates the application document (model)
// Called by the Symbian application framework when the application is started.
// Needed for all Symbian applications.
CApaDocument* C$(Basename)Application::CreateDocumentL() {
	return new (ELeave) C$(Basename)Document(*this);
}

// Specifies the application UID, needed for all Symbian applications.
TUid C$(Basename)Application::AppDllUid() const {
	return KUid$(Basename)App;
}

//----------------------------------------------------------------------------
// C$(Basename)Document (The Model of the MVC)
//----------------------------------------------------------------------------

// Creates the application document (Symbian terminology for model).
// Needed for all Symbian applications, even if the application doesn't
// save or load files.
C$(Basename)Document::C$(Basename)Document(CEikApplication& aApp) : CEikDocument(aApp) {
}

// Creates the application UI class.
// Called by the application framework when the application needs to start
// handling events.
// Needed for all Symbian GUI applications.
CEikAppUi* C$(Basename)Document::CreateAppUiL() {
	return new (ELeave) C$(Basename)AppUi;
}

//----------------------------------------------------------------------------
// C$(Basename)AppUi (The Controller of the MVC)
//----------------------------------------------------------------------------

// Second-phase construction function.
// Called by the Symbian application framework after calling
// the constructor from C$(Basename)Document::CreateAppUiL().
// Needed for initialising the application views.
void C$(Basename)AppUi::ConstructL(void) {
	// Load the application resources defined in the $(Basename).rss, e.g.
	// application menus, dialogs etc.
	BaseConstructL();

	iAppContainer = new (ELeave) C$(Basename)Container;
	iAppContainer->SetMopParent( this );
	iAppContainer->ConstructL( ClientRect() );
	AddToStackL( iAppContainer );
}

// Releases resources used by the application UI.
// Called when shutting down the application.
C$(Basename)AppUi::~C$(Basename)AppUi() {
	if (iAppContainer)
	{
		RemoveFromStack( iAppContainer );
		delete iAppContainer;
	}
}

// Handles user commands such as those associated to the menu entries.
// Called by CEikAppUi::ProcessCommandL().
// Application commands are defined in the $(Basename).hrh file.
// Common commands are defined for instance in the uikon.hrh file, located
// under the system include directory (%EPOCROOT%\epoc32\include).
void C$(Basename)AppUi::HandleCommandL(TInt aCommand) {
	switch (aCommand)
	{
	case EEikCmdFileOpen:
		{
		iEikonEnv->InfoMsg(_L("Open"));
		CAknInformationNote* note = new (ELeave) CAknInformationNote;
		note->ExecuteLD(_L("$(Message)"));
		break;
		}
	case EAknSoftkeyBack:
	case EEikCmdExit:
		{
		Exit();
		break;
		}
	default:
		break;
	}
}

//----------------------------------------------------------------------------
// C$(Basename)Container (The View of the MVC)
//----------------------------------------------------------------------------

// Second-phase construction function.
// Initializes the container (creates a window, sets its bounds and activates
// it) and creates the child/nested controls (if any).
// Called from the AppUi after creating a new container object.
void C$(Basename)Container::ConstructL(const TRect& aRect) {
	CreateWindowL();

	iLabel = new (ELeave) CEikLabel;
	iLabel->SetContainerWindowL( *this );
	iLabel->SetTextL( _L("Example View") );

	iToDoLabel = new (ELeave) CEikLabel;
	iToDoLabel->SetContainerWindowL( *this );
	iToDoLabel->SetTextL( _L("Add Your controls\n here") );

	SetRect(aRect);
	ActivateL();
}

// Destructor used for releasing the resources used by his container.
// Called from the AppUi's destructor.
C$(Basename)Container::~C$(Basename)Container() {
	delete iLabel;
	delete iToDoLabel;
}

// Lays out the child controls (if any).
// Called when the control is resized.
void C$(Basename)Container::SizeChanged() {
	// TODO: Add here control resize code etc.
	iLabel->SetExtent( TPoint(10,10), iLabel->MinimumSize() );
	iToDoLabel->SetExtent( TPoint(10,100), iToDoLabel->MinimumSize() );
}

// Returns the number of child controls, 0 if none.
TInt C$(Basename)Container::CountComponentControls() const {
	return 2; // return nbr of controls inside this container
}

// Returns the child control at the given index, NULL if none.
CCoeControl* C$(Basename)Container::ComponentControl(TInt aIndex) const {
	switch ( aIndex )
	{
	case 0:
		return iLabel;
	case 1:
		return iToDoLabel;
	default:
		return NULL;
	}
}

// Draws to this controls canvas using graphics primitives.
// Note that the UI framework takes care of drawing the child controls.
void C$(Basename)Container::Draw(const TRect& aRect) const {
	CWindowGc& gc = SystemGc();
	// TODO: Add your drawing code here
	// example code...
	gc.SetPenStyle( CGraphicsContext::ENullPen );
	gc.SetBrushColor( KRgbGray );
	gc.SetBrushStyle( CGraphicsContext::ESolidBrush );
	gc.DrawRect( aRect );
}

// Handles events generated by the child controls.
void C$(Basename)Container::HandleControlEventL(CCoeControl* /*aControl*/,TCoeEvent /*aEventType*/) {
	// TODO: Add your control event handler code here
}

